<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="设备设施名称" prop="deviceName">
                    <el-input
                        v-model="queryParams.deviceName"
                        placeholder="请输入设备设施名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="设备设施编号" prop="deviceNo">
                    <el-input
                        v-model="queryParams.deviceNo"
                        placeholder="请输入设备设施编号"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="设备设施状态" prop="deviceState">
                    <el-input
                        v-model="queryParams.deviceState"
                        placeholder="请输入设备设施状态"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>

                  <el-form-item label="设备设施责任人" prop="deviceUser">
                    <el-input
                        v-model="queryParams.deviceUser"
                        placeholder="请输入设备设施责任人"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="设备设施购买时间" prop="deviceBuyTime">
                    <el-date-picker clearable
                                    v-model="queryParams.deviceBuyTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择设备设施购买时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="设备设施保修期限" prop="deviceWarrantyPeriod">
                    <el-date-picker clearable
                                    v-model="queryParams.deviceWarrantyPeriod"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择设备设施保修期限">
                    </el-date-picker>
                  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['community:manage:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['community:manage:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['community:manage:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['community:manage:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
              <el-table-column label="设备设施名称" align="center" prop="deviceName"/>
              <el-table-column label="设备设施编号" align="center" prop="deviceNo"/>
              <el-table-column label="设备设施类型" align="center" prop="deviceType"/>
              <el-table-column label="设备设施状态" align="center" prop="deviceState"/>
              <el-table-column label="设备设施位置" align="center" prop="deviceLocation"/>
              <el-table-column label="设备设施责任人" align="center" prop="deviceUser"/>
              <el-table-column label="设备设施购买时间" align="center" prop="deviceBuyTime" width="150">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.deviceBuyTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="设备设施保修期限" align="center" prop="deviceWarrantyPeriod" width="150">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.deviceWarrantyPeriod, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="设备设施维护记录" align="center" prop="deviceMaintainRecords"/>
              <el-table-column label="设备设施使用说明书" align="center" prop="deviceUseDesc"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['community:manage:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['community:manage:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改设备设施管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
                        <el-form-item label="设备设施名称" prop="deviceName">
                          <el-input v-model="form.deviceName" placeholder="请输入设备设施名称"/>
                        </el-form-item>
                        <el-form-item label="设备设施编号" prop="deviceNo">
                          <el-input type="number" v-model="form.deviceNo" placeholder="请输入设备设施编号"/>
                        </el-form-item>
                        <el-form-item label="设备设施状态" prop="deviceState">
                          <el-select v-model="form.deviceState" placeholder="请选择设备设施状态">
                            <el-option label="正常" value="正常"></el-option>
                            <el-option label="故障" value="故障"></el-option>
                            <el-option label="维修中" value="维修中"></el-option>
                            <el-option label="报废" value="报废"></el-option>
                          </el-select>
                        </el-form-item>

                        <el-form-item label="设备设施类型" prop="deviceType">
                          <el-select v-model="form.deviceType">
                            <el-option v-for="(dbType,index) in deviceTypes" :key="index" :value="dbType">{{ dbType }}</el-option>
                          </el-select>
                        </el-form-item>

                        <el-form-item label="设备设施位置" prop="deviceLocation">
                          <el-input v-model="form.deviceLocation" placeholder="请输入设备设施位置"/>
                        </el-form-item>
                        <el-form-item label="设备设施责任人" prop="deviceUser">
                          <el-input v-model="form.deviceUser" placeholder="请输入设备设施责任人"/>
                        </el-form-item>
                        <el-form-item label="设备设施购买时间" prop="deviceBuyTime">
                          <el-date-picker clearable
                                          v-model="form.deviceBuyTime"
                                          type="date"
                                          value-format="yyyy-MM-dd"
                                          placeholder="请选择设备设施购买时间">
                          </el-date-picker>
                        </el-form-item>
                        <el-form-item label="设备设施保修期限" prop="deviceWarrantyPeriod">
                          <el-date-picker clearable
                                          v-model="form.deviceWarrantyPeriod"
                                          type="date"
                                          value-format="yyyy-MM-dd"
                                          placeholder="请选择设备设施保修期限">
                          </el-date-picker>
                        </el-form-item>
                        <el-form-item label="设备设施维护记录" prop="deviceMaintainRecords">
                          <el-input v-model="form.deviceMaintainRecords" type="textarea" placeholder="请输入内容"/>
                        </el-form-item>
                        <el-form-item label="设备设施使用说明书" prop="deviceUseDesc">
                          <el-input v-model="form.deviceUseDesc" placeholder="请输入设备设施使用说明书"/>
                        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addManage, delManage, getDeviceType, getManage, listManage, updateManage} from "@/api/community/manage";

  export default {
    name: "Manage",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 设备设施管理表格数据
              manageList: [],
        //设备设施类型
              deviceTypes: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
                        deviceName: null,
                        deviceNo: null,
                        deviceType: null,
                        deviceState: null,
                        deviceLocation: null,
                        deviceUser: null,
                        deviceBuyTime: null,
                        deviceWarrantyPeriod: null,
                        deviceMaintainRecords: null,
                        deviceUseDesc: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
                        deviceMaintainRecords: [
                    { required: true, message: "设备设施维护记录不能为空", trigger: "blur" }
                  ],
                        deviceUseDesc: [
                    { required: true, message: "设备设施使用说明书不能为空", trigger: "blur" }
                  ],
                        delFlag: [
                    { required: true, message: "删除标志不能为空", trigger: "blur" }
                  ],
                        createBy: [
                    { required: true, message: "创建者不能为空", trigger: "blur" }
                  ],
                        createTime: [
                    { required: true, message: "创建时间不能为空", trigger: "blur" }
                  ],
                        updateBy: [
                    { required: true, message: "更新者不能为空", trigger: "blur" }
                  ],
                        updateTime: [
                    { required: true, message: "更新时间不能为空", trigger: "blur" }
                  ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {

      /** 查询设备设施管理列表 */
      getList() {
        this.loading = true;
        listManage(this.queryParams).then(response => {
          this.manageList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
                        deviceId: null,
                        deviceName: null,
                        deviceNo: null,
                        deviceType: null,
                        deviceState: null,
                        deviceLocation: null,
                        deviceUser: null,
                        deviceBuyTime: null,
                        deviceWarrantyPeriod: null,
                        deviceMaintainRecords: null,
                        deviceUseDesc: null,
                        delFlag: null,
                        createBy: null,
                        createTime: null,
                        updateBy: null,
                        updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.deviceId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();

        getDeviceType("device_type").then(response=>{
          this.deviceTypes=response.data;
        });
        this.open = true;
        this.title = "添加设备设施管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const deviceId = row.deviceId || this.ids
        getManage(deviceId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改设备设施管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.deviceId != null) {
              updateManage(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addManage(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const deviceIds = row.deviceId || this.ids;
        this.$modal.confirm('是否确认删除设备设施管理编号为"' + deviceIds + '"的数据项？').then(function() {
          return delManage(deviceIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
  /** 导出按钮操作 */
  handleExport() {
    this.download('community/manage/export', {
      ...this.queryParams
    }, `manage_${new Date().getTime()}.xlsx`)
  }
  }
  };
</script>
